//
//  ReadViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "ReadViewController.h"


@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)NSMutableArray *dataArray;//轮播图数组
@property(nonatomic,strong)NSMutableArray  *dataSource;//数据源数组
@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ReadViewController

- (NSMutableArray *)dataArray
{
    //你大爷的
    if (!_dataArray) {
        _dataArray = [NSMutableArray array] ;
    }
    return _dataArray   ;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //设置item大小
        layout.itemSize  =CGSizeMake(125,125);
        //设置间距
        layout.sectionInset = UIEdgeInsetsMake(10, 5, 5, 10);
        _collectionView = [[UICollectionView  alloc] initWithFrame:CGRectMake(0, kheight/3 +64, kwidth, kheight) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate  = self;
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets  = NO;
    self.view.backgroundColor = [UIColor  grayColor];
    self.collectionView.backgroundColor  =[UIColor  grayColor];
    
    [self.view addSubview:self.collectionView];
    
    [RequestManager fetchDataWithURL:kReadUrl  parameters:@{} method:@"GET" complet:^(NSData *data) {
        NSDictionary  * dic  =[NSJSONSerialization  JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        //scroll数组
        for (NSDictionary  *subDic in dic[@"data"][@"carousel"]) {
            [self.dataArray addObject:subDic[@"img"]];
        }
        
        //数据源
        for (NSDictionary *subDic  in dic[@"data"][@"list"]) {
            ReadModel *model = [[ReadModel alloc] init];
            [model  setValuesForKeysWithDictionary:subDic];
            [self.dataSource  addObject:model];
        }
        
    } updataUI:^{
        
        //新建的 轮播图
        AdvertisingScrollView *headerView = [[AdvertisingScrollView alloc]initWithMinFrame:CGRectMake(0, 64, kwidth, kheight/3-64)];
        [headerView  initializationUIWithDataArray:self.dataArray];
        [headerView setAnimationDuration:2];
        [self.view  addSubview:headerView];
        
        [self.collectionView reloadData];
        NSLog(@"刷新UI");
    }];
    
    //注册XIB
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:    [NSBundle  mainBundle]] forCellWithReuseIdentifier:@"read_cell"];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
    [cell setCellWIthModel:self.dataSource[indexPath.row]];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //通过XIB获取
    ReadList *readListVC = [[ReadList alloc] initWithNibName:@"ReadList" bundle:nil];
    ReadModel *model = self.dataSource[indexPath.row];
    readListVC.typeID = model.type;
    
    [self.navigationController pushViewController:readListVC animated:YES];
}


@end
